package com.example.doctor.controller;

import com.example.common.Dto;
import com.example.common.DtoUtil;
import com.example.doctor.service.ArticleService;
import com.example.doctor.service.CommentService;
import com.example.pojo.GdArticle;
import com.example.pojo.GdComment;
import com.example.pojo.GdInfoDic;
import com.example.utils.ErrorCode;
import com.example.vo.ArticleReturnVo;
import com.example.vo.ArticleVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LENOVO on 2018/9/23.
 */
@RestController
@RequestMapping("/api")
public class ArticleController {

    @Resource
    private ArticleService articleService;
    /**
     * 文章分类查询
     * @return
     */
    @ApiOperation(value="文章分类查询",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="文章分类查询")
    @RequestMapping(value = "/getArticleType", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto getArticleType(HttpServletResponse response)
    {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map map=new HashMap<String,Object>();
        List<GdInfoDic> infoDicList1=null;
        List<GdInfoDic> infoDicList2=null;
        try {
            Map map1=new HashMap<String,Object>();
            map1.put("parentId",24);
            infoDicList1=articleService.getArticleType(map1);
            for(int i=0;i<infoDicList1.size();i++)
            {
                map1.clear();
                map1.put("parentId",25+i);
                infoDicList2=articleService.getArticleType(map1);
                map.put(infoDicList1.get(i).getName(), infoDicList2);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("文章分类查询失败！", ErrorCode.SEARCH_abnormal_FAILED);
        }
        if(map!=null)
        {
            return DtoUtil.returnSuccess("文章分类查询成功!",map);
        }
        else
        {
            return DtoUtil.returnSuccess("没有文章分类数据，请重新查询",map);
        }
    }

    /**
     * 文章列表
     * @return
     */
    @ApiOperation(value="文章列表查询",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="文章列表查询")
    @RequestMapping(value = "/getArticleList", produces = "application/json", method = RequestMethod.POST)
    @ResponseBody
    public Dto getArticleList(ArticleVO articleVO,HttpServletResponse response)
    {
        response.setHeader("Access-Control-Allow-Origin", "*");
        List<ArticleReturnVo> gdArticleVoList=null;
        try {
            gdArticleVoList=articleService.getArticleList(articleVO);
        }
        catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取文章列表失败！", ErrorCode.SEARCH_abnormal_FAILED);
        }
        if(gdArticleVoList!=null)
        {
            return DtoUtil.returnSuccess("获取文章列表成功!",gdArticleVoList);
        }
        else
        {
            return DtoUtil.returnSuccess("没有文章列表数据，请重新查询",gdArticleVoList);
        }
    }

    /**
     * 文章详情
     * @return
     */
    @ApiOperation(value="文章详情",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="文章详情")
    @RequestMapping(value = "/getArticleDetleById/{id}", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto getArticleDetleById(@PathVariable String id ,HttpServletResponse response)
    {
        response.setHeader("Access-Control-Allow-Origin", "*");
       ArticleReturnVo articleReturnVo=null;
        try {
            articleReturnVo=articleService.getArticleDetleById(id);
        }
        catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取文章详情失败！", ErrorCode.SEARCH_abnormal_FAILED);
        }
        if(articleReturnVo!=null)
        {
            return DtoUtil.returnSuccess("获取文章详情成功!",articleReturnVo);
        }
        else
        {
            return DtoUtil.returnSuccess("没有文章详情数据，请重新查询",articleReturnVo);
        }
    }
}
